package com.bootdo.app;

import com.bootdo.common.controller.BaseController;
import com.bootdo.common.domain.FileDO;
import com.bootdo.common.service.FileService;
import com.bootdo.common.utils.R;
import com.bootdo.common.utils.UuidUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Controller
@RequestMapping("/app/file/")
public class AppFileController extends BaseController {

    @Autowired
    private FileService fileService;

    /***
     * 图片上传
     * @param file
     * @param fileDO
     * @return
     */
    @PostMapping("/upload")
    @ResponseBody
    public R upload(MultipartFile[] file, FileDO fileDO) {
        R r = new R();
        r = fileService.upload(file,fileDO);
        return r;
    }

    /***
     * 获取文件列表数据
     * @param attachId
     * @return
     */
    @PostMapping("/list")
    @ResponseBody
    public List<FileDO> list(String attachId) {
        List<FileDO> list = fileService.queryListByAttachId(attachId);
        return list;
    }

    /***
     * 获取随机数
     * @return
     */
    @PostMapping("/getUUID")
    @ResponseBody
    public R getUUID() {
        String attachId = UuidUtils.getUUID();
        return R.ok().put("attachId",attachId);
    }

    /***
     * 图片删除
     * @param fileDO
     * @return
     */
    @PostMapping("/delete")
    @ResponseBody
    public R delete(FileDO fileDO) {
        fileService.remove(fileDO);
        return R.ok();
    }
}
